package com.test.lucene.helper;

import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;

/**
 * Read the command line and return the parameters.
 * 
 * @author Patrick Kimber
 * 
 */
public final class WriteParameters extends SharedParameters {

    /** Command line parameters. */
    private static final String OPTION_CREATE = "create";

    /** Command line parameters. */
    private static final String OPTION_PREFIX = "prefix";

//    private static final String OPTION_LOCK_PREFIX = "lockPrefix";

    /**
     * Constructor.
     * 
     * @param name
     *            The name of the main class.
     * @param params
     *            Command line parameters.
     */
    public WriteParameters(final String name, final String[] params) {
        super(name, params);
    }

    /**
     * Get the prefix which is used to build a unique key for documents in this
     * batch.
     * 
     * @return The prefix.
     */
    public String getPrefix() {
        return getCommandLine().getOptionValue(OPTION_PREFIX);
    }

//    public String getLockPrefix() {
//      return getCommandLine().getOptionValue(OPTION_LOCK_PREFIX);
//    }

    /**
     * Does the Lucene repository need to be created?
     * 
     * @return <code>true</code>if the Lucene repository needs to be created.
     */
    public boolean isCreate() {
        return getCommandLine().hasOption(OPTION_CREATE);
    }

    /**
     * Has the user specified the correct command line parameters?
     * 
     * @return <code>true</code> If the user has displayed the correct command
     *         line parameters.
     */
    public boolean isValid() {
        boolean result = getCommandLine().hasOption(OPTION_MINUTES)
                && getCommandLine().hasOption(OPTION_PREFIX)
                && getCommandLine().hasOption(OPTION_REPOSITORY);
//                && getCommandLine().hasOption(OPTION_LOCK_PREFIX);
        return result;
    }

    /**
     * Get the options for the application.
     * 
     * @return The command line options.
     */
    protected Options getOptions() {
        Option create = new Option(OPTION_CREATE, "create a new repository.");
        Option prefix = OptionBuilder.withArgName(OPTION_PREFIX).hasArg()
                .withDescription("unique key for this batch of documents.")
                .create(OPTION_PREFIX);
        Options options = getSharedOptions();
        options.addOption(create);
        options.addOption(prefix);
//        options.addOption(OptionBuilder.withArgName(OPTION_LOCK_PREFIX).hasArg().withDescription("used to verify locking integrity").create(OPTION_LOCK_PREFIX));
        return options;
    }
}
